<template>
  <div>
<!--    <h1>侧边栏1号</h1>  -->
    <div style="margin: 10px 0">
      <el-date-picker
          v-model="search"
          prefix-icon="el-icon-date"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH-mm-ss"
      ></el-date-picker>
<!--      <el-input placeholder="请输入内容" v-model="search" clearable style="width: 15%"> </el-input>-->
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button style="margin-left: 10px" @click="addRecomcate">新增</el-button>
    </div>
    <el-table
        :data="tableData"
        border
        style="width: 100%"
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }">
      <el-table-column
          label="序号"
          type="index"
          width="50">
      </el-table-column>
      <el-table-column
          prop="recom_cateCode"
          label="编号">
      </el-table-column>
      <el-table-column
          prop="recom_cateName"
          label="名称">
      </el-table-column>
      <el-table-column
          prop="shop_create_time"
          label="日期">
      </el-table-column>
      <el-table-column prop="recom_state"  label="上架" min-width="25%">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.recom_state"
              :active-value="1"
              :inactive-value="2"
              @change="switchover(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope" >
          <el-button
              size="medium" type="warning"
              @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
              size="medium"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!-- dialog弹窗 -->
    <el-dialog title="新增页面" :visible.sync="dialogVisible" width="30%">
      <el-form :model="shopRecomcate" label-width="120px">
        <el-form-item label="名称">
          <el-input v-model="shopRecomcate.recom_cateName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="编号">
          <el-input v-model="shopRecomcate.recom_cateCode" style="width: 80%"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addsubmit">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  inject: ['reload'], //注入reload方法
  name: "OneSidebar",
  data() {
    return {
      search:[],
      currentPage: 1,
      pagesize:10,
      total: 0,
      tableData: [
        {
          recom_cateCode: '',
          recom_cateName: '',
          shop_create_time: ''
        }
      ],
      dialogVisible: false,//dialog弹窗
      shopRecomcate:{},
    }
  },
  created() {
    //页面打开加载方法
    this.load();
  },
  methods:{
    load() {
      //日期选择框重新清空
      if (!this.search) {
        this.search = []
      }
      this.$axios({
        method:'post',
        url:'/api/user/selectRecomcateAll',
        data:{
          limit: this.pagesize,
          page : this.currentPage,
          startTime : this.search[0],
          endTime : this.search[1],
        }
      }).then((resp) =>{
        console.log(resp);
        this.tableData = resp.data.rows
        this.total = resp.data.total
        if (resp.data.code === 401){
          this.$message.error({message: resp.data.msg});
        }
      }).catch((error) =>{
        console.log(error);   //请求失败返回的数据
      })
    },
    addRecomcate(){

      this.dialogVisible = true;
      this.shopRecomcate = {};
    },
    addsubmit(){
      if (this.shopRecomcate.id){//此时有id的话提交按钮为更新
        console.log("修改按钮调用--->id为" + this.shopRecomcate.id)
        this.load();//刷新表格数据
        this.dialogVisible = false;
      }else {//新增
        console.log("新增按钮调用")
      }

      // request.post("/user/selectRecomcateAll",this.shopRecomcate).then(res => {
      // console.log(res)
      // })
    },
    handleEdit(row){
      //编辑按钮  避免浅拷贝的问题
      this.shopRecomcate = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      console.log(row)
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      this.load();//刷新表格数据
      this.dialogVisible = false;
    },
    handleSizeChange(pagesize){//改变当前每页的个数触发
      this.pagesize = pagesize
      this.load();//刷新表格数据
    },
    handleCurrentChange(currentPage){//改变当前页码触发
      this.currentPage = currentPage
      this.load();//刷新表格数据
    },
    switchover(data){
      //切换按钮状态
      let msg;
      if (data.recom_state == "2"){
        msg = "下架"
      }else {
        msg = "上架"
      }
      this.$confirm('确认将此条商品修改为'+ msg+'吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios({
          method:'post',
          url:'/api/user/updateState',
          data:{
            id : data.id,
            recom_state : data.recom_state
          }
        }).then((resp) =>{
          if (resp.data.success){
            this.$message({message: resp.data.msg, type: 'success'});
          }else {
            this.$message.error({message: resp.data.msg});
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
        this.reload();//刷新
      });


    }
  }
}
</script>

<style scoped>

</style>
